package com.hk.servlet.admin;

import com.alibaba.fastjson.JSONObject;
import com.hk.model.Admin;
import com.hk.utils.CharacterEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: zhe
 * @Date: 2021/6/5 18:27
 * @Statement: 登录账户信息
 */
@WebServlet("/admin/loginAdmin")
public class LoginAdminServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        统一字符编码 返回头类型
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        String sessionId = session.getId();
//        System.out.println("获取到JssionId=："+sessionId);
//        System.out.println("获取到的登录用户信息："+admin);
        if (admin == null) {
            out.println(0);
        } else {
            admin = new Admin(admin.getId(),admin.getName(),admin.getPassword());
            String adminJSON = JSONObject.toJSONString(admin);
            out.println(adminJSON);
        }
        out.flush();
        out.close();
    }
}
